package cn.mxz.interceptor;


import cn.mxz.utils.JwtUtils;
import cn.mxz.utils.Rsautils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.PublicKey;
import java.util.Map;

/**
 * 作者；朱洪旭
 * 时间；2021/11/26 15:59
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //放行请求方法为OPTIONS的请求
        String method = request.getMethod();
        if(method.equals("OPTIONS")){
            return  true;
        }
        String token = request.getHeader("token");
        try {
            PublicKey publicKey11 = Rsautils.getPublicKey1(new ClassPathResource("/rsa.pub").getInputStream());;
            JwtUtils.getInfoFromToken(token, publicKey11, Map.class);
            return  true;
        }catch (Exception e){
            response.sendRedirect(request.getContextPath()+"/inte");
            return  false;
        }
    }
}
